import { Camera, geometry, Vec2 } from 'cc';
import { PhysicsSystem } from 'cc';
import { _decorator, Component, Node, director, Scene } from 'cc';
import { EventDispatcher } from '../../ngame/util/EventDispatcher';
import GameConst from '../GameConst';
import { IClick } from '../scene/IClick';
const { ccclass, property } = _decorator;


/**
 * 屏幕触摸管理器
 */
@ccclass('TouchsManage')
export class TouchsManage extends Component {

    @property({ type:Node, displayName:"检索节点" })
    search:Node|Scene = null;

    //场景相机
    @property({ type:Camera, displayName:"场景相机" })
    worldCamera:Camera = null;

    //UI相机
    @property({ type:Camera, displayName:"UI相机" })
    uiCamera:Camera = null;

    onLoad() {

        //找到@Touch关键字
        this.search || (this.search = director.getScene());

        let children = this.search.children;

        children.forEach((child) => {

            if(child.name.indexOf(`@Touch`) == -1) return;

            //绑定事件
            if(child.name.indexOf(`click`) != -1 && child.name.indexOf(`WorldTouch`) != -1){
                //绑定点击事件
                child.on(Node.EventType.TOUCH_START,(event) => {
                    EventDispatcher.getIns().emit(GameConst.TOUCH.TOUCH_WORLD_CLICK,event)
                });

            }
            if(child.name.indexOf(`wheel`) != -1 && child.name.indexOf(`WorldTouch`) != -1){
                //绑定点击事件
                child.on(Node.EventType.MOUSE_WHEEL,(event) => {
                    EventDispatcher.getIns().emit(GameConst.TOUCH.TOUCH_WORLD_WHEEL,event)
                });

            }
            if(child.name.indexOf(`move`) != -1 && child.name.indexOf(`WorldTouch`) != -1){
                //绑定点击事件
                child.on(Node.EventType.TOUCH_MOVE,(event) => {
                    EventDispatcher.getIns().emit(GameConst.TOUCH.TOUCH_WORLD_MOVE,event)
                });

            }
            
        })

        this.onEvent();

    }
    onDestroy(){

        this.offEvent();

    }

    onEvent(){
        EventDispatcher.getIns().on(GameConst.TOUCH.TOUCH_WORLD_CLICK,this.touchWorldClick,this);
    }

    offEvent(){
        EventDispatcher.getIns().off(GameConst.TOUCH.TOUCH_WORLD_CLICK,this.touchWorldClick,this);
    }

    touchWorldClick(event){

        console.log("clickWorld");

        let ray = new geometry.Ray();
        this.worldCamera.screenPointToRay(event.getLocationX(), event.getLocationY(), ray);
        // 以下参数可选
        const mask = 0xffffffff;
        const maxDistance = 100000000000;
        const queryTrigger = true;
        
        if (PhysicsSystem.instance.raycastClosest(ray, mask, maxDistance, queryTrigger)) {
            const raycastClosestResult = PhysicsSystem.instance.raycastClosestResult;

            let click:IClick = null;
            if(click = raycastClosestResult.collider.getComponent(IClick)){
                EventDispatcher.getIns().emit(GameConst.TOUCH.TOUCH_WORLD_CLICK_OBJECT,click,raycastClosestResult);
            }else{
                EventDispatcher.getIns().emit(GameConst.TOUCH.TOUCH_WORLD_CLICK_FLOOR,raycastClosestResult);
            }
            
            // // this.moveWorldPlayer(raycastClosestResult.hitPoint.clone())
            // this.rServerEvent("moveWorldPlayer",raycastClosestResult.hitPoint.clone());
            
        }
        
    }

    //返回世界位置
    getUIWorldPosition(event){
        let ray = new geometry.Ray();
        this.worldCamera.screenPointToRay(event.getLocationX(), event.getLocationY(), ray);

        // 以下参数可选
        const mask = 0xffffffff;
        const maxDistance = 100000000000;
        const queryTrigger = true;

        if (PhysicsSystem.instance.raycastClosest(ray, mask, maxDistance, queryTrigger)) {
            const raycastClosestResult = PhysicsSystem.instance.raycastClosestResult;
            return raycastClosestResult
        }

        return null;

    }

    update(deltaTime: number) {
        
    }

}

